package com.uenpay.im.ui.old.activity.group;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;

/**
 * @author SL
 * @created 2016-3-4 上午11:49:21
 * @copyRight http://www.qiyunxin.com
 * @function 未加入群聊详情
 */
public class NotJoinGroupActivity extends BaseActivity {

    private GroupMangerHandler mGroupMangerHandler = new GroupMangerHandler();
    private String chatId, chatName;
    private int custNum;
    private RoundImageView avatarIv;
    private TextView nameTv, sizeTv;
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_not_join_group;
    }

    @Override
    public void initView(View view) {
        chatId = getIntent().getStringExtra("chat_id");
        chatName = getIntent().getStringExtra("chat_name");
        custNum = getIntent().getIntExtra("cust_name", 0);
        initView();
        initListener();
    }

    protected void initView() {
        loading = findViewById(R.id.loading);
        avatarIv = (RoundImageView) findViewById(R.id.avatar_iv);
        nameTv = (TextView) findViewById(R.id.name_tv);
        sizeTv = (TextView) findViewById(R.id.group_size_tv);
        ((TextView) findViewById(R.id.title_textview)).setText("");
        avatarIv.SetUrl(APIConfiguration.getAvatarUrlNormal(chatId,
                PacketType.GROUP));
        nameTv.setText(chatName);
        sizeTv.setText(MessageFormat.format(
                getResources().getString(R.string.total_count), custNum));
    }

    protected void initListener() {
        findViewById(R.id.apply_join_group_btn).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        if (!TextUtils.isEmpty(chatId)) {
                            joinGroup();
                        }
                    }
                });
    }

    private void joinGroup() {
        mGroupMangerHandler.autoJoinGroup(chatId, new GroupMangerHandler.IAutoJoinListener() {

            @Override
            public void onAutoJoinResult(int status, String msg) {
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    ActivityHandle.startChat(NotJoinGroupActivity.this,
                            Long.valueOf(chatId), PacketType.GROUP, 0,
                            chatName, 0, null);
                    finish();
                } else if (TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });
    }
}
